package io.renren.service.impl;

import cn.hutool.core.io.unit.DataUnit;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.hutool.core.util.StrUtil;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.DateUtils;
import io.renren.dao.AutowashmDao;
import io.renren.dao.ProjectmDao;
import io.renren.dao.UcmlContactDao;
import io.renren.dao.UcmlOrganizeDao;
import io.renren.dto.AutowashmDTO;
import io.renren.dto.WashDetailDto;
import io.renren.entity.AutowashmEntity;
import io.renren.entity.ProjectmEntity;
import io.renren.entity.UcmlContactEntity;
import io.renren.entity.UcmlOrganizeEntity;
import io.renren.service.AutowashmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * ${comments}
 *
 * @author Sen 524227263@qq.com
 * @since 1.0.0 2024-05-28
 */
@Service
public class AutowashmServiceImpl extends CrudServiceImpl<AutowashmDao, AutowashmEntity, AutowashmDTO> implements AutowashmService {

    @Override
    public QueryWrapper<AutowashmEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<AutowashmEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Autowired
    private UcmlOrganizeDao ucmlOrganizeDao;

    @Autowired
    private UcmlContactDao ucmlContactDao;

    @Autowired
    private ProjectmDao projectmDao;


    @Override
    public Map<String, Object> washDetail(String traderMOID, WashDetailDto dto) {
        Map<String, Object> result = new HashMap<>();

        AutowashmEntity info = baseDao.selectOne(new QueryWrapper<AutowashmEntity>()
                .eq("AutoWashMOID", dto.getAutoWashMoId()));
        if (info != null) {
            result.put("serviceDate", DateUtils.format(info.getBilldate(), "yyyy-MM-dd HH:mm:ss"));

            UcmlOrganizeEntity organizeEntity = ucmlOrganizeDao.selectOne(new QueryWrapper<UcmlOrganizeEntity>()
                    .select("OrgName").eq("UCML_OrganizeOID", info.getOrgFk()));
            result.put("orgName", organizeEntity != null ? organizeEntity.getOrgname() : "");

            UcmlContactEntity ucmlContactEntity = ucmlContactDao.selectOne(new QueryWrapper<UcmlContactEntity>()
                    .select("PersonName").eq("UCML_CONTACTOID", info.getUcmlContactFk()));
            result.put("PersonName", ucmlContactEntity != null ? ucmlContactEntity.getPersonname() : "");

            List<Object> goodsList = new ArrayList<>();
            result.put("goodsList", goodsList);

            result.put("BusinessAmount", new BigDecimal(info.getTotalamount()).setScale(2, BigDecimal.ROUND_HALF_UP));
            result.put("vipDiscount", new BigDecimal(info.getDiscountamount()).setScale(2, BigDecimal.ROUND_HALF_UP));
            result.put("couponDiscount", 0);
            result.put("Amount", info.getPrice().setScale(2, BigDecimal.ROUND_HALF_UP));

            List<Map<String, Object>> proList = projectmDao.selectMaps(new QueryWrapper<ProjectmEntity>()
                    .select("Name,Name as projectName,CostAmount as BusinessAmount,CostAmount as Amount")
                    .eq("ProjectMOID", info.getProjectmFk()));
            result.put("proList", proList);

        }
        return result;
    }

    @Override
    public List<AutowashmEntity> findListByAutoWashMOID(String fk) {
        List<AutowashmEntity> proList = this.baseDao.selectList(
                new QueryWrapper<AutowashmEntity>()
                        .select("ProjectM_FK,TotalAmount,DiscountAmount,Amount")
                        .eq("AutoWashMOID", fk));
        return proList;
    }
}